<?php

// amespace Library\PurviewClass;
class Purview {

    public function LoadModel($model) {
        $ModelFile = APPLICATION_PATH . '/module/' . $model . 'Model.php';
        if (file_exists($ModelFile)) {
            include_once ($ModelFile);
            $class = $model . 'Model';
            return new $class();
        } else {
            $ModelFile = (IS_DEBUG == true ? $ModelFile : $model);
            ErrorMsg::Debug('Model:[' . $ModelFile . ']文件不存在');
        }
    }

    public static function LoadHelper($helper) {
        $HelperFile = APPLICATION_PATH . '/helper/' . $helper . '.php';
        if (file_exists($HelperFile)) {
            include_once ($HelperFile);
        } else {
            $HelperFile = (IS_DEBUG == true ? $HelperFile : $helper);
            ErrorMsg::Debug('Helper:[' . $HelperFile . ']文件不存在');
        }
    }
    
    public static function checkPurview($uid = '0', $route_key = array()){
        if(empty($route_key) || !is_array($route_key)) return false;
        self::LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        $member = $MemberHelper->getMember($uid);
        self::LoadHelper('PurviewListHelper');
        $PurviewListHelper = new PurviewListHelper();
        $PurviewArray = $PurviewListHelper->getKeyArr($route_key[0]);
        $route_key = implode('_', $route_key);
        if(isset($PurviewArray[$route_key]['id']) && in_array($PurviewArray[$route_key]['id'], $member['role'])){
            return true;
        }else{
            return false;
        }
        
    }

    public static function checkRole($uid = '0',$role_arr = array()){
        if(empty($role_arr) || !is_array($role_arr)) return false;
        self::LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        $member = $MemberHelper->getMember($uid);
        if(in_array($member['u_role'],$role_arr)){
            return true;
        }else{
            return false;
        }
    }

    public static function checkScope($uid = '0',$scope= array()){
        if(empty($scope) || !is_array($scope)) return false;
        self::LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        $member = $MemberHelper->getMember($uid);
        if(in_array($member['u_scope'],$scope)){
            return true;
        }else{
            return false;
        }
    }

    public static function IsAllow($GroupId = '0', $Module = 'index', $Controller = 'index', $Action = 'index', $UserConfig = '') {
        self::LoadHelper('RoleHelper');
        $RoleHelper = new RoleHelper();
        $roleRow = $RoleHelper->getId($GroupId);

        if (empty($roleRow['r_id'])) {
            return false;
        }
        $site_role = empty($roleRow['role']) ? array() : $roleRow['role'];
        if (empty($site_role)) {
            return false;
        }

        return self::DefaultAllow($site_role, $Controller, $Action, $Module);
        return false;
    }

    public static function DefaultAllow($GroupRole, $Controller = 'index', $Action = 'index', $Module = 'index') {
        self::LoadHelper('PurviewListHelper');
        $PurviewListHelper = new PurviewListHelper();
        $PurviewArray = $PurviewListHelper->GetArray($Module);
        $ControllerId = $ActionId = '';
        if (!empty($PurviewArray[0])) {
            foreach ($PurviewArray[0] as $key => $value) {
                if ($PurviewArray[$value['id']]) {
                    foreach ($PurviewArray[$value['id']] as $k => $v) {
                        if ($Controller == $v['code']) {
                            $ControllerId = $v['id'];
                        }
                    }
                }
            }
            if (in_array($ControllerId, $GroupRole)) {
                if (!empty($PurviewArray[$ControllerId])) {
                    foreach ($PurviewArray[$ControllerId] as $key => $value) {
                        if ($Action == $value['code']) {
                            if (in_array($value['id'], $GroupRole)) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    public static function EnabledIp() {
        $ip = ip2long(defined('C_IP') ? C_IP : Client::GetIp());
        $CleanIP = include CONFIG_PATH . '/IP.php';
        /*         *
         * xxx.xxx.xxx.xxx 比如 127.0.0.2
         * xxx.xxx.xxx.* 比如 1270.0.0.*
         * xxx.xxx.xxx.yyy-zzz 比如 127.0.0.2-23
         * */
        $Check = true;
        if (empty($CleanIP['enabled'])) {
            return $Check;
        }
        foreach ($CleanIP['enabled'] as $value) {
            $value = str_replace('*', '0-255', $value);
            $ips1 = $ips2 = array();
            $ips = explode('.', trim($value));
            for ($i = 0; $i < 4; $i++) {
                if (preg_match('/-/', $ips[$i])) {
                    $i = explode('-', trim($ips[$i]));
                    $ips1[] = $i[0];
                    $ips2[] = $i[1];
                } else {
                    $ips1[] = $ips[$i];
                    $ips2[] = $ips[$i];
                }
            }
            $Check = false;
            $ip1 = ip2long(implode('.', $ips1));
            $ip2 = ip2long(implode('.', $ips2));
            if ($ip1 <= $ip && $ip <= $ip2) {
                return true;
            }
        }
        return $Check;
    }

    public static function DisabledIp() {
        $ip = ip2long(defined('C_IP') ? C_IP : Client::GetIp());
        $CleanIP = include CONFIG_PATH . '/IP.php';
        /*         *
         * xxx.xxx.xxx.xxx 比如 127.0.0.2
         * xxx.xxx.xxx.* 比如 1270.0.0.*
         * xxx.xxx.xxx.yyy-zzz 比如 127.0.0.2-23
         * */
        $Check = true;
        if (empty($CleanIP['disabled'])) {
            return $Check;
        }
        foreach ($CleanIP['disabled'] as $value) {
            $value = str_replace('*', '0-255', $value);
            $ips1 = $ips2 = array();
            $ips = explode('.', trim($value));
            for ($i = 0; $i < 4; $i++) {
                if (preg_match('/-/', $ips[$i])) {
                    $i = explode('-', trim($ips[$i]));
                    $ips1[] = $i[0];
                    $ips2[] = $i[1];
                } else {
                    $ips1[] = $ips[$i];
                    $ips2[] = $ips[$i];
                }
            }
            $Check = true;
            $ip1 = ip2long(implode('.', $ips1));
            $ip2 = ip2long(implode('.', $ips2));
            if ($ip1 <= $ip && $ip <= $ip2) {
                return false;
            }
        }
        return $Check;
    }

    public static function checkDptype($uid= '0',$type = array()){
        if(empty($type) || !is_array($type)) return false;
        self::LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        $member = $MemberHelper->getMember($uid);
        if(in_array($member['u_dp_type'],$type)){
            return true;
        }else{
            return false;
        }
    }

}